package vue;

import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.UIManager;

import controleur.ToogleCellule;

import model.Cellule;
import model.Coordonnee;
import model.Environnement;
import model.Rules;

public class VCellule extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Coordonnee coord;

	public VCellule(Coordonnee coord, Environnement env, JPanel grid, PanneauPrincipal panneauPrincipal) {
		super();
		this.addMouseListener(new ToogleCellule(env, grid, panneauPrincipal));
		this.addMouseMotionListener(new ToogleCellule(env, grid, panneauPrincipal));
		this.coord = coord;
		Cellule cel = env.getCellule(coord);
		if (cel == null)
			mort();
		else
			vie(cel.getLife());
	}

	/**
	 * @return the coord
	 */
	public Coordonnee getCoord() {
		return coord;
	}
	
	public void mort() {
		this.setBorder(BorderFactory.createLineBorder(Color.lightGray));		
		setBackground((Color) UIManager.get("JPanel.background"));
	}

	public void vie(int life) {
		int b = (int) (511 *((float)life / Rules.getMaxLife()));
		Color c;
		if(b < 256)
		 c = new Color(255, b, 0);
		else
			c = new Color(255 - (b - 256), 255, 0);
		setBackground(c);
		this.setBorder(BorderFactory.createEmptyBorder());

	}
}
